package com.xuelang.pipeline.artifact.infrastructure.resource.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.RepositoryPackageDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * 制品仓库-包 数据Mapper
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Repository
public interface RepositoryPackageMapper extends MPJBaseMapper<RepositoryPackageDO> {

    /**
     * 更新制品包版本和数量
     *
     * @param name
     * @param group
     * @param format
     * @param version
     */
    @Update({
            "UPDATE IM_OT_DO_REPOSITORY_PACKAGE SET LATEST_VERSION = #{version}, COMPONENT_COUNT = IF(COMPONENT_COUNT - 1, COMPONENT_COUNT - 1, 0) ",
            "WHERE 1=1 ",
            "<if test='instanceRepositoryName !=null'> ",
            "	AND INSTANCE_REPOSITORY_NAME = #{instanceRepositoryName} ",
            "</if> ",
            "<if test='name !=null'> ",
            "	AND NAME_ID = #{name} ",
            "</if> ",
            "<if test='group !=null'> ",
            "	AND GROUP_ID = #{group} ",
            "</if> ",
            "<if test='format !=null'> ",
            "	AND FORMAT = #{format} ",
            "</if> "
    })
    void updateVersionAndCount(@Param("instanceRepositoryName") String instanceRepositoryName, @Param("name") String name, @Param("group") String group, @Param("format") String format, @Param("version") String version);

    /**
     * 更新制品包版本和数量
     *
     * @param packageName
     * @param version
     * @param format
     */
    @Update({
            "UPDATE IM_OT_DO_REPOSITORY_PACKAGE SET LATEST_VERSION = #{version}, COMPONENT_COUNT = IF(COMPONENT_COUNT - 1, COMPONENT_COUNT - 1, 0) ",
            "WHERE 1=1 ",
            "<if test='instanceRepositoryName !=null'> ",
            "	AND INSTANCE_REPOSITORY_NAME = #{instanceRepositoryName} ",
            "</if> ",
            "<if test='packageName !=null'> ",
            "	AND PACKAGE_NAME = #{packageName} ",
            "</if> ",
            "<if test='format !=null'> ",
            "	AND FORMAT = #{format} ",
            "</if> "
    })
    void updateVersionAndCountByPackageName(@Param("instanceRepositoryName") String instanceRepositoryName, @Param("packageName") String packageName, @Param("version") String version, @Param("format") String format);
}
